/**
 * 
 */
package gameMiniGames;
import java.awt.Component;
import javax.swing.*;
import java.awt.event.*;   
import gameUtils.*;
import gameExceptions.*;

public class Towers extends JLayeredPane implements MouseListener,MouseMotionListener{
	
	static final long serialVersionUID = 0;
	//Since Towers is a singleton class... must hold it own instance and then call through getInstance
	public Towers INSTANCE = this;

	//JOptionPane
	public JOptionPane winner;
	public boolean played = false;
	//Images of Importance
	public  JLabel background = new JLabel(new ImageIcon("src/gameMiniGames/TowerImages/TowerOfHanoi.jpg"));
	public  JLabel DiskOne = new JLabel(new ImageIcon("src/gameMiniGames/TowerImages/Disk1.png"));
	public  JLabel DiskTwo = new JLabel (new ImageIcon("src/gameMiniGames/TowerImages/Disk2.png"));
	public  JLabel DiskThree = new JLabel( new ImageIcon("src/gameMiniGames/TowerImages/Disk3.png"));
	public  JLabel DiskFour = new JLabel (new ImageIcon("src/gameMiniGames/TowerImages/Disk4.png"));
	public  JLabel DiskFive = new JLabel(new ImageIcon("src/gameMiniGames/TowerImages/Disk5.png"));
	
	//HotSpot
	public TowerHotspot hotspotListener; 
	
	//DataStructures
	public  MinLockedSelectionStack PegOne = new MinLockedSelectionStack(1);
	public  MinLockedSelectionStack PegTwo = new MinLockedSelectionStack(2);
	public  MinLockedSelectionStack PegThree = new MinLockedSelectionStack(3);
	
	//Nodes
	public  LockedSelectionStackNode NodeThree = new LockedSelectionStackNode(1, DiskThree, null, 3);
	public  LockedSelectionStackNode NodeTwo = new LockedSelectionStackNode(1, DiskTwo, NodeThree, 2);
	public  LockedSelectionStackNode NodeOne = new LockedSelectionStackNode(1, DiskOne, NodeTwo, 1);
	public  LockedSelectionStackNode NodeFour = new LockedSelectionStackNode(1, DiskFour, NodeThree, 4);
	public  LockedSelectionStackNode NodeFive = new LockedSelectionStackNode(1, DiskFive, NodeFour, 5);
	   
	//Integers
	public  int xAdjustment, yAdjustment;
	public  int gameYpos = 255, pegOneX=190, pegTwoX = 310, pegThreeX = 427;
	
	//Coordinates
	public  Coordinate draggedFrom, draggedTo, releasePoint;
	
	//DragComponent
	public  Component DragComponent;
	
	
	public Towers(){
		//Create the LayeredPane
		//layers = getLayeredPane();
		
		//Add the game pieces to the board
		add(background, JLayeredPane.DEFAULT_LAYER, -1);
		add(DiskFive, JLayeredPane.DRAG_LAYER);
		add(DiskFour, JLayeredPane.DRAG_LAYER);
		add(DiskThree, JLayeredPane.DRAG_LAYER);
		add(DiskTwo, JLayeredPane.DRAG_LAYER);
		add(DiskOne, JLayeredPane.DRAG_LAYER);
		addMouseListener(this);
		addMouseMotionListener(this);		
		
//		Set Bounds
		background.setBounds(0,0,680,480);
		DiskOne.setBounds(pegOneX+20,gameYpos-18-21-18-12,39,19);
		DiskTwo.setBounds(pegOneX+15,gameYpos-21-18-13,51,18);
		DiskThree.setBounds(pegOneX+10,gameYpos-16-18,65,21);
		DiskFour.setBounds(pegOneX+5,gameYpos-18,86,26);
		DiskFive.setBounds(pegOneX,gameYpos,101,18);
				
//		Fill up PegOne with lotsa info
		try{
			PegOne.push(NodeFive);
			PegOne.push(NodeFour);
			PegOne.push(NodeThree);
			PegOne.push(NodeTwo);
			PegOne.push(NodeOne);
		}catch(MinLockException $e){
			System.out.println("Error");
			System.err.println($e.getMessage());
		}
		
		hotspotListener = new TowerHotspot(this);
		
		System.out.println(PegOne.toString());
		System.out.println(PegTwo.toString());
		System.out.println(PegThree.toString());
		
		
		
	}
	
	public void mouseClicked(MouseEvent $e){}
	public void mouseEntered(MouseEvent $e){}
	public void mouseExited(MouseEvent $e){}
	public void mousePressed(MouseEvent $e){
		
		DragComponent = null;
		Component c = null;
		

		if(findComponentAt($e.getX(), $e.getY()) != background && findComponentAt($e.getX(),$e.getY())!= null){
				
			c =  findComponentAt($e.getX(), $e.getY());
			System.out.println(c.toString());
			if (c instanceof JLayeredPane){ 
				return;
			}
		
			draggedFrom = new Coordinate($e.getX(),$e.getY());
			
			if(c == null){
				System.out.println("Ive got nothing");
				return;
			}else{
				
				System.out.println(DragComponent);
				if(!PegOne.isEmpty()){
					if(c == PegOne.peek().piece){
						System.out.println(PegOne.peek().toString());
						System.out.println(c.toString());
						DragComponent = c;
						xAdjustment = DragComponent.getLocation().x - $e.getX();
						yAdjustment = DragComponent.getLocation().y - $e.getY();
						DragComponent.setLocation($e.getX() + xAdjustment, $e.getY() + yAdjustment);
						moveToFront(DragComponent);
					}
				}
				if(!PegTwo.isEmpty()){
					if(c == PegTwo.peek().piece){
						DragComponent = c;
						xAdjustment = DragComponent.getLocation().x - $e.getX();
						yAdjustment = DragComponent.getLocation().y - $e.getY();
						DragComponent.setLocation($e.getX() + xAdjustment, $e.getY() + yAdjustment);
						moveToFront(DragComponent);
					}
				}
				if(!PegThree.isEmpty()){
					if(c == PegThree.peek().piece){
						DragComponent = c;
						xAdjustment = DragComponent.getLocation().x - $e.getX();
						yAdjustment = DragComponent.getLocation().y - $e.getY();
						DragComponent.setLocation($e.getX() + xAdjustment, $e.getY() + yAdjustment);
						moveToFront(DragComponent);
					}
			
				}
			}
		}
		
	}
	public void mouseReleased(MouseEvent $e){
		
		if(DragComponent == null){
			System.out.println("Ive got Nothing");
			return;
		}else{
			releasePoint = new Coordinate($e.getX(),$e.getY());
			hotspotListener.Intersects(releasePoint);
			if(wonGame()){
				JOptionPane.showMessageDialog(this, new String("You WIN!!"), new String("Congratulations"), 1);
			}else{
				//Continue the game...
			}
		}
		
	}
	public void mouseMoved(MouseEvent $e){	
		   
	}
	public void mouseDragged(MouseEvent $e){
		if (DragComponent != null){		  
			DragComponent.setLocation($e.getX() + xAdjustment, $e.getY() + yAdjustment);
		}
	}
	
	public boolean wonGame(){
		return (PegThree.size == 5 || PegTwo.size == 5)?true:false;
	}
	public boolean beenPlayed()
	{
		return played;
	}
	
	public void setPlayed(boolean p)
	{
		played = p;
	}
	
}
	
